<?php


namespace app\api\model;


use app\exceptions\ApiException;
use ba\Exception;
use think\facade\Db;

/**
 * Banner
 */
class Merchant
{


    protected $status = [
        1 => '预约已提交，正在处理中',
        2 => "预约已通过",
        3 => "用户已到店",
        4 => "用户已退房",
        5 => "系统判定爽约",
        6 => "系统已取消"
    ];


    public function merchantInfo($params)
    {
        $member_id = $params['member_id'] ?? '';
        //查询民宿信息
        $res = (new Hotel())->where(['member_id' => $member_id])->find();
        return api_message($res);
    }


    public function getOrderNums($params)
    {
        $member_id = $params['member_id'] ?? '';
        $hotel_id = (new Hotel())->where(['member_id' => $member_id])->value('id');
        if (!$hotel_id) {
            return api_message([
                'total_nums' => 0,
                'check_nums' => 0,
                'confirm_nums' => 0,
                'complete_nums' => 0
            ]);
        }


        $map[] = ['hotel_id', '=', $hotel_id];
        $map[] = ['status', '>', 0];
        $res = (new HotelOrder())->where($map)
            ->field(
                ' 
           count(id) as total_nums,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as check_nums,
            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as confirm_nums,
            SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) as complete_nums
            '
            )->select();

        return api_message($res);
    }


    public function getOrderList($params)
    {
        $member_id = $params['member_id'] ?? '';
        $hotel_id = (new Hotel())->where(['member_id' => $member_id])->value('id');
        $type = $params['type'] ?? 0;
        $search = $params['search'] ?? '';

        $map[] = ['ho.hotel_id', '=', $hotel_id];
        $map[] = ['ho.status', '>', 0];
        if ($type) {
            $map[] = ['ho.status', '=', $type];
        }

        if ($search) {
            $map[] = ['m.username|m.phone', 'like', "%" . $search . "%"];
        }

        $res = (new HotelOrder())->alias('ho')
            ->join('member m', 'm.id = ho.member_id')
            ->where($map)
            ->field('ho.id as order_id,m.avatar,m.username,m.phone,ho.order_start_time,ho.order_end_time,ho.status')
            ->paginate(10);

        foreach ($res as &$val) {
            $val['avatar'] = full_url($val['avatar'], true, config('buildadmin.default_avatar'));
            $val['date'] = date('Y-m-d', $val['order_start_time']) . ' 至 ' . date('Y-m-d', $val['order_end_time']);

            unset($val['order_start_time'], $val['order_end_time']);
        }

        return api_message($res);
    }


    public function getOrderDetail($params)
    {
        $order_id = $params['order_id'] ?? 0;
        $order = (new HotelOrder())->alias('ho')
            ->join('hotel h', 'h.id = ho.hotel_id')
            ->join('member m', 'm.id = ho.member_id')
            ->where(['ho.id' => $order_id])
            ->field('ho.id as order_id,h.name,h.cover,h.hotel_cate_ids,m.username,m.phone,m.id_number,ho.status')
            ->find();

        if (!$order) {
            throw new ApiException('数据不存在');
        }
        $order['cate_name'] = \app\admin\model\hotel\Cate::whereIn('id', $order['hotel_cate_ids'])->column('name');
        $order['cover'] = full_url($order['cover']);
        unset($order['hotel_cate_ids']);

        //获取入住信息
        $order['status_text'] = (new OrderLog())->getStatusText($order_id, '2');
        return api_message($order);
    }


    public function getOrderStatus($params)
    {
        $order_id = $params['order_id'] ?? 0;
        $status = $params['status'] ?? '';
        if (!$order_id || !$status) {
            throw  new ApiException('缺少参数');
        }
        $order = (new HotelOrder())->where(['id' => $order_id])->find();
        if (!$order) {
            throw new ApiException('数据不存在');
        }
        try {
            Db::startTrans();
            (new HotelOrder())->where(['id' => $order_id])->update(['status' => $status]);
            (new OrderLog())->addLog(2, $order_id, $status, $this->status[$status]);
            Db::commit();
            return api_message([],'操作成功');
        } catch (Exception $e) {
            Db::rollBack();
            throw new ApiException($e->getMessage());
        }
    }


}